package ks.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import java.util.List;
import ks.dao.CourseDAO;
import ks.dao.TeacherDAO;
import ks.po.Course;
import ks.po.Teacher;

//@WebServlet(name = "TeacherServlet")
@WebServlet( "/TeacherServlet")
public class TeacherServlet extends HttpServlet
{
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        //doPost方法会被多个表单调用 ,查询老师,增加老师,增加课程  因此这里需要根据不同表单传递的标示参数调用不同的方法
        String m = request.getParameter("m");
        if("search".equals(m))
        {
            search(request, response);
        }
        else if("addSpec".equals(m))
        {
            addSpec(request, response);
        }
        else if("addTeacher".equals(m))
        {
            addTeacher(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        try
        {
            String mode = request.getParameter("mode");//获得从teachersearch.jsp或teachersearch_result.jsp的超链接传递来的mode参数
            if("deleteTeacher".equals(mode))
            {
//                new TeacherDAO().delete(Integer.parseInt(request.getParameter("teacherId")));
                String strTeacherId = request.getParameter("teacherId");
                int teacherId = Integer.parseInt(strTeacherId);
                TeacherDAO teacherDAO = new TeacherDAO();
                teacherDAO.delete(teacherId);
                request.setAttribute("msg", "成功删除老师："+request.getParameter("teacherName"));
                request.getRequestDispatcher("/teachersearch.jsp").forward(request, response);
            }
            else if("newSpec".equals(mode))
            {
                request.getRequestDispatcher("/courseAdd.jsp").forward(request, response);
            }
            else if("newTeacher".equals(mode))
            {
                request.setAttribute("courses", new CourseDAO().getAll());
                request.getRequestDispatcher("/teacheradd.jsp").forward(request, response);
            }
            else
            {
                request.getRequestDispatcher("/teachersearch.jsp").forward(request, response);
            }
        }
        catch (Exception e)
        {
            request.setAttribute("msg", e.getMessage());
            request.getRequestDispatcher("/teachersearch.jsp").forward(request, response);
        }
    }

    private void search(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
    {
        String vname=request.getParameter("vname");
        String sname=request.getParameter("sname");
        try
        {
            List<Teacher> teachers=new TeacherDAO().search(vname, sname);
            if(0==teachers.size())
            {
                request.setAttribute("msg", "没有相关老师信息");
                request.getRequestDispatcher("/teachersearch.jsp").forward(request, response);
            }
            else
            {
                request.setAttribute("teachers", teachers);
                request.getRequestDispatcher("/teachersearch_result.jsp").forward(request, response);
            }
        }
        catch (Exception e)
        {
            request.setAttribute("msg", e.getMessage());
            request.getRequestDispatcher("/teachersearch.jsp").forward(request, response);
        }
    }

    private void addSpec(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
    {
        Course course = new Course();
        String courseName = request.getParameter("courseName");
        String courseDesc = request.getParameter("courseDesc");
        if("".equals(courseName))
        {
            request.setAttribute("msg", "请输入课程名称");
            request.getRequestDispatcher("/courseAdd.jsp").forward(request, response);
        }
        else
        {
            course.setName(courseName);
            course.setDesc(courseDesc);
            try
            {
                new CourseDAO().save(course);
                request.setAttribute("msg", "添加成功");
                request.getRequestDispatcher("/teachersearch.jsp").forward(request, response);
            }
            catch (Exception e)
            {
                request.setAttribute("msg",e.getMessage());
                doGet(request,response);
            }
        }
    }

    private void addTeacher(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
    {
       try
       {
           //这里需要根据表单封装一个Teacher   表单里有老师名  课程id下拉列表
           Teacher teacher = new Teacher();
           //根据下拉列表封装多个课程
           String[] sids = request.getParameterValues("sid");
           String vname = request.getParameter("vname");
           if ("".equals(vname))
           {
//            request.setAttribute("msg", "请输入老师姓名");
//            这里虽然要返回的teacheradd.jsp提示消息 但是不能直接转发到teacheradd.jsp  因为teacheradd.jsp需要课程集合显示数据
//			   request.getRequestDispatcher("/teacheradd.jsp").forward(request, response);
//            doGet(request, response);
               request.setAttribute("msg", "请输入老师姓名");
               request.setAttribute("courses", new CourseDAO().getAll());
               request.getRequestDispatcher("/teacheradd.jsp").forward(request, response);// 跳转到teacheradd.js,by hlzhang, 20180418
           }
           else if (null == sids || 0 == sids.length)
           {
//            request.setAttribute("msg", "请选择课程");
//            doGet(request, response);
               request.setAttribute("msg", "请选择课程");
               request.setAttribute("courses", new CourseDAO().getAll());
               request.getRequestDispatcher("/teacheradd.jsp").forward(request, response);// 跳转到teacheradd.js,by hlzhang, 20180418
           }
           else
           {
               teacher.setName(vname);
               for (String sid : sids)
               {
                   Course s = new Course();
                   s.setId(Integer.parseInt(sid));
                   teacher.getcourses().add(s);
               }
               new TeacherDAO().save(teacher);
               request.setAttribute("msg", "添加成功");
               request.getRequestDispatcher("/teachersearch.jsp").forward(request, response);
           }
       }
       catch (Exception e)
       {
           request.setAttribute("msg",e.getMessage());
           doGet(request,response);
       }

    }
}
